/**
 * transferStore.jsp引用
 * @param $
 * @param win
 * @param doc
 */
var url = '/business/transferStoreServlet';
var sumType = 10;
(function($, win, doc){
	// 查询条件
	var createCondCombox = function(){
		var param = Param.getReadCmd();
		var cond = {};
		param = Param.buildParam(param,cond);
		param[Param.CMDTYPEKEY] = 0;
		param[Param.CMDVALUEKEY] = 1;
		
		// 调出仓库
		param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 1;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		$('#storeOut').combobox({
			url: CONTEXT_PATH + Global.commonUrl,
			queryParams : param,
			method : 'post',
		    valueField: 'value',
		    textField: 'text',
		    loader: Util.combox.loader
		}).combobox('setValue', '-');
		
		/*param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 1;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		var param = {
				shopType:"'加盟','直营'",
				zoneCode:''
		}
		$('#storeOut').combobox({
			prompt:'请输入或选择分店', 
	        url: CONTEXT_PATH + '/ktcount/getShop',
	        queryParams:param,
			method : 'post',
		    valueField: 'SHOP_ID',
		    textField: 'SHOP_NAME',
		    groupField:'SHOP_TYPE',
            multiple:true,
		    loader: Util.combox.loader,
		    groupFormatter: function(group){
				return '<span style="color:red">' + group + '</span>';
			},
		    onClick: function(node) { 
                attributeShow($("#shop").combotree("getValue")); 
            }, 
            filter: function(q, row){ 
                var opts = $(this).combobox('options'); 
                //return row[opts.textField].indexOf(q) == 0; 
                return row[opts.textField].indexOf(q)>-1;//将从头位置匹配改为任意匹配 
            }, 
		    formatter: function (row) {
                var opts = $('#storeOut').combobox('options');
                '<input type="checkbox" class="combobox-checkbox">' + '全部'
                return '<input type="checkbox" class="combobox-checkbox">' + row[opts.textField]
            },
            onLoadSuccess: function () { //下拉框数据加载成功调用
                var opts = $('#storeOut').combobox('options');
                var target = this;
                var values = $(target).combobox('getValues');//获取选中的值的values
                $.map(values, function (value) {
                    var el = opts.finder.getEl(target, value);
                    el.find('input.combobox-checkbox')._propAttr('checked', true); 
                })
            },
            onSelect: function (row) { //选中一个选项时调用
                var opts = $('#storeOut').combobox('options');
                //获取选中的值的values
                $("#storeOut").val($('#storeOut').combobox('getValues'));
			   //设置选中值所对应的复选框为选中状态
                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', true);
            },
            onUnselect: function (row) {//不选中一个选项时调用
                var opts = $('#storeOut').combobox('options');
                //获取选中的值的values
                $("#storeOut").val($('#storeOut').combobox('getValues'));
              
                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', false);
            },
		});*/
		// 调入仓库
		param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 1;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		$('#storeIn').combobox({
			url: CONTEXT_PATH + Global.commonUrl,
			queryParams : param,
			method : 'post',
		    valueField: 'value',
		    textField: 'text',
		    loader: Util.combox.loader
		}).combobox('setValue', '-');
		
		/*param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 1;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		var param = {
				shopType:"'加盟','直营'",
				zoneCode:''
		}
		$('#storeIn').combobox({
			prompt:'请输入或选择分店', 
	        url: CONTEXT_PATH + '/ktcount/getShop',
	        queryParams:param,
			method : 'post',
		    valueField: 'SHOP_ID',
		    textField: 'SHOP_NAME',
		    groupField:'SHOP_TYPE',
            multiple:true,
		    loader: Util.combox.loader,
		    groupFormatter: function(group){
				return '<span style="color:red">' + group + '</span>';
			},
		    onClick: function(node) { 
                attributeShow($("#storeIn").combotree("getValue")); 
            }, 
            filter: function(q, row){ 
                var opts = $(this).combobox('options'); 
                //return row[opts.textField].indexOf(q) == 0; 
                return row[opts.textField].indexOf(q)>-1;//将从头位置匹配改为任意匹配 
            }, 
		    formatter: function (row) {
                var opts = $('#storeIn').combobox('options');
                '<input type="checkbox" class="combobox-checkbox">' + '全部'
                return '<input type="checkbox" class="combobox-checkbox">' + row[opts.textField]
            },
            onLoadSuccess: function () { //下拉框数据加载成功调用
                var opts = $('#storeIn').combobox('options');
                var target = this;
                var values = $(target).combobox('getValues');//获取选中的值的values
                $.map(values, function (value) {
                    var el = opts.finder.getEl(target, value);
                    el.find('input.combobox-checkbox')._propAttr('checked', true); 
                })
            },
            onSelect: function (row) { //选中一个选项时调用
                var opts = $('#storeIn').combobox('options');
                //获取选中的值的values
                $("#storeIn").val($('#storeIn').combobox('getValues'));
			   //设置选中值所对应的复选框为选中状态
                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', true);
            },
            onUnselect: function (row) {//不选中一个选项时调用
                var opts = $('#storeIn').combobox('options');
                //获取选中的值的values
                $("#storeIn").val($('#storeIn').combobox('getValues'));
              
                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', false);
            },
		});*/
		// 商品类型
		param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 2;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		$('#goodsType').combobox({
			url: CONTEXT_PATH + Global.commonUrl,
			queryParams : param,
			method : 'post',
		    valueField: 'value',
		    textField: 'text',
		    loader: Util.combox.loader
		}).combobox('setValue', '-');
		// 品牌
		param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 4;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		$('#brand').combobox({
			url: CONTEXT_PATH + Global.commonUrl,
			queryParams : param,
			method : 'post',
		    valueField: 'value',
		    textField: 'text',
		    loader: Util.combox.loader
		}).combobox('setValue', '-');
	};
	/**
	 * 创建条件对象
	 */
	var getCond = function() {
		var cond = {};
		// 时间
		var startTime = $('#startTime').datebox('getValue');
		cond['startTime'] = Param.buildParamItem('=', startTime, Param.SQLType.VARCHAR); 
		var endTime = $('#endTime').datebox('getValue');
		cond['endTime'] = Param.buildParamItem('=', endTime, Param.SQLType.VARCHAR); 
		//调出仓库
		var storeOut = $('#storeOut').combobox('getValue');
		cond['storeOut'] = Param.buildParamItem('=', storeOut, Param.SQLType.VARCHAR); 
		//调入仓库
		var storeIn = $('#storeIn').combobox('getValue');
		cond['storeIn'] = Param.buildParamItem('=', storeIn, Param.SQLType.VARCHAR); 
		//商品类型
		var goodsType = $('#goodsType').combobox('getValue');
		cond['goodsType'] = Param.buildParamItem('=', goodsType, Param.SQLType.VARCHAR); 
		//品牌
		var brand = $('#brand').combobox('getValue');
		cond['brand'] = Param.buildParamItem('=', brand, Param.SQLType.VARCHAR); 
		//货号
		var artNo = $('#artNo').val();
		if(artNo==""){
			artNo = "-";
		}
		cond['artNo'] = Param.buildParamItem('=', '%'+artNo+'%', Param.SQLType.VARCHAR); 
		//批次号
		var batchNo = $('#batchNo').val();
		if(batchNo==""){
			batchNo = "-";
		}
		cond['batchNo'] = Param.buildParamItem('=', '%'+batchNo+'%', Param.SQLType.VARCHAR); 
		return cond;
	};
	// 查询
	var onQuery = function(){
		var cond = getCond();
		if(!Util.nvl(cond) && cond == false){
			return ;
		}
		var param = Param.getReadCmd();
		param = Param.buildParam(param, cond);
		var dgCol;
		if(sumType==10){
			param[Param.CMDTYPEKEY] = 0;
			dgCol = [[
				    {field: '_rn',title: '行号',align: 'center',width: 30
				    	,formatter: function(value, row, index) {
				    		return index+1;
				    	}
					},
					{field: 'OUT_SHOP_ID',title: '调出仓库编码',width: 80,align:'center'},
				    {field: 'OUT_SHOP_NAME',title: '调出仓库名称',width: 150,align:'center'},
				    {field: 'IN_SHOP_ID',title: '调入仓库编码',width: 80,align:'center'},
				    {field: 'IN_SHOP_NAME',title: '调入仓库名称',width: 150,align:'center'},
				    {field: 'PRODUCT_CODE',title: '货号',width: 100,align:'center'},
				    {field: 'PRODUCT_NAME',title: '品名',width: 100,align:'center'},
				    {field: 'SPEC',title: '规格',width: 50,align:'center'},
				    {field: 'UNIT',title: '单位',width: 50,align:'center'},
				    {field: 'CST_PRICE',title: '成本单价',width: 70,align:'center'},
				    {field: 'RTL_PRICE',title: '售价',width: 70,align:'center'},
				    {field: 'QUANTITY',title: '数量',width: 70,align:'center'},
				    {field: 'OUT_RTL_PRICE_AMOUNT',title: '成本金额',width: 70,align:'center'},
				    {field: 'BATCH_NO',title: '批次号',width: 150,align:'center'},
				    {field: 'CREATE_TIME',title: '转仓时间',width: 100,align:'center'},
				    {field: 'PRODUCT_TYPE_ID',title: '类别编码',width: 60,align:'center'},
				    {field: 'PROD_TYPE_NAME',title: '类别名称',width: 60,align:'center'},
				    {field: 'BRAND_ID',title: '品牌编码',width: 60,align:'center'},
				    {field: 'BRAND_NAME',title: '品牌名称',width: 60,align:'center'}
			]];
		}else if(sumType==11){
			param[Param.CMDTYPEKEY] = 1;
			dgCol = [[
					    {field: '_rn',title: '行号',align: 'center',width: 30
					    	,formatter: function(value, row, index) {
					    		return index+1;
					    	}
						},
						{field: 'OUT_SHOP_ID',title: '调出仓库编码',width: 110,align:'center'},
					    {field: 'OUT_SHOP_NAME',title: '调出仓库名称',width: 220,align:'center'},
					    {field: 'IN_SHOP_ID',title: '调入仓库编码',width: 40,align:'center'},
					    {field: 'IN_SHOP_NAME',title: '调入仓库名称',width: 60,align:'center'},
					    {field: 'CST_PRICE',title: '成本单价',width: 70,align:'center'},
					    {field: 'RTL_PRICE',title: '售价',width: 70,align:'center'},
					    {field: 'QUANTITY',title: '数量',width: 70,align:'center'},
					    {field: 'OUT_RTL_PRICE_AMOUNT',title: '金额',width: 70,align:'center'},
				]];
		}
		param = Param.buildParam(param, cond);
		$('#dg').datagrid({
			url: CONTEXT_PATH +url,
			queryParams: param,
			method: 'POST',
			fit: false,
			columns:dgCol
		});
	};
	// 导出excel
	var onExpExcel = function() {
		var cond = getCond();
		if (!cond) {
			return false;
		}
		var param = Param.getExportCmd();
		param = Param.buildParam(param, cond);
		if(sumType==10){
			param[Param.CMDTYPEKEY] = 0;
		}else if(sumType==11){
			param[Param.CMDTYPEKEY] = 1;
		}
		Util.doPost(url, param, function(data) {
			if (data && data.file) {
				win.open(data.file);
			} else if (data && data.stateMsg) {
				Util.msg.alert(data.stateMsg);
				return false;
			}
		});
	};
	//页面导出
	   var daochu = function(){   
		   var startTime = $('#startTime').datebox('getValue');	
		   var endTime = $('#endTime').datebox('getValue');	
		   var area = $('#storeIn').combobox('getText');
		   var shop = $('#storeOut').combobox('getText');
		   if(area == "" || area == null){
			   area = ""
		   }
		   if(shop == "" || shop == null){
			   shop = ""
		   }
	  	  exportExcel.jqgridtoExcel($(".datagrid-header"),$(".datagrid-body"),'转仓查询报表',startTime,endTime,area,shop);
	   }
	
	// 初始化
	$(function(){
		// dg
		$('#dg').datagrid({
			fit: true,
			columns: [[
			    {field: '_rn',title: '行号',align: 'center',width: 30
				    	,formatter: function(value, row, index) {
				    		return index+1;
				    	}
				},
				{field: 'OUT_SHOP_ID',title: '调出仓库编码',width: 80,align:'center'},
			    {field: 'OUT_SHOP_NAME',title: '调出仓库名称',width: 150,align:'center'},
			    {field: 'IN_SHOP_ID',title: '调入仓库编码',width: 80,align:'center'},
			    {field: 'IN_SHOP_NAME',title: '调入仓库名称',width: 150,align:'center'},
			    {field: 'PRODUCT_CODE',title: '货号',width: 100,align:'center'},
			    {field: 'PRODUCT_NAME',title: '品名',width: 100,align:'center'},
			    {field: 'SPEC',title: '规格',width: 50,align:'center'},
			    {field: 'UNIT',title: '单位',width: 50,align:'center'},
			    {field: 'CST_PRICE',title: '成本单价',width: 70,align:'center'},
			    {field: 'RTL_PRICE',title: '售价',width: 70,align:'center'},
			    {field: 'QUANTITY',title: '数量',width: 70,align:'center'},
			    {field: 'OUT_RTL_PRICE_AMOUNT',title: '金额',width: 70,align:'center'},
			    {field: 'BATCH_NO',title: '批次号',width: 150,align:'center'},
			    {field: 'PRODUCT_TYPE_ID',title: '类别编码',width: 60,align:'center'},
			    {field: 'PROD_TYPE_NAME',title: '类别名称',width: 60,align:'center'},
			    {field: 'BRAND_ID',title: '品牌编码',width: 60,align:'center'},
			    {field: 'BRAND_NAME',title: '品牌名称',width: 60,align:'center'}
			]]
		});
		//创建条件查询下拉组件
		createCondCombox();
		// 初始化时间
		var cdate = Util.date.format(new Date(), 'yyyy-MM-dd');
		$('#startTime').datebox('setValue', cdate);
		$('#endTime').datebox('setValue', cdate);
		
		$(":radio").click(function(){
			var radioValue = $(this).val();
			pubFunction.setTime(radioValue);
			if(radioValue>9){
				sumType = radioValue;
			}else{
				pubFunction.setTime(radioValue);
			}
	    });
		
		// 执行查询
		$('#queryBtn').off('click').on('click', onQuery);
		// 执行导出
		//$('#exportBtn').on('click', onExpExcel);
		$('#exportBtn').on('click', daochu); 
		// 执行退出
		$('#closeBtn').on('click', pubFunction.CloseWebPage);
		// 执行打印
		$('#printBtn').on('click', pubFunction.PrintPage);

	});
})(jQuery, window, document);
